package com.bhf.controller;

import com.baomidou.mybatisplus.core.metadata.OrderItem;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.bhf.annotation.OperationLog;
import com.bhf.model.entity.SysUser;
import com.bhf.model.es.Person;
import com.bhf.service.SysUserService;
import com.bhf.utils.ElasticsearchUtil;
import com.bhf.utils.RestResult;
import com.bhf.utils.RestResultUtils;
import org.springframework.security.crypto.password.PasswordEncoder;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

import javax.annotation.Resource;
import java.io.IOException;
import java.time.LocalDateTime;


/**
 * @author pwq
 */
@RestController
public class SysUserController {

    @Resource
    private SysUserService sysUserService;
    @Resource
    private PasswordEncoder passwordEncoder;
    @Resource
    private ElasticsearchUtil elasticsearch;

    @PostMapping("/register/info")
    @OperationLog("添加用户")
    public RestResult<Boolean> register(@RequestBody SysUser user){
        user.setPassword(passwordEncoder.encode(user.getPassword()));
        return RestResultUtils.success(sysUserService.saveOrUpdate(user));
    }

    @GetMapping("/register/list")
    @OperationLog("查询用户列表")
    public RestResult<Page<SysUser>> list() throws IOException {
        Person person = new Person();
        person.setName("张三222222211");
        person.setPassword("433");
        person.setCreateTime(LocalDateTime.now());
        boolean b = elasticsearch.delete("bhf-data-index","-5ydf3oBvZWCiJ4o10Xf");
        Page<SysUser> page = new Page<>(1, 8);
        page.addOrder(OrderItem.desc("create_time"));
        return RestResultUtils.success(sysUserService.page(page));
    }
}
